package com.activity;

import java.util.ArrayList;
import java.util.List;

import com.bean.BimestreBean;
import com.bean.DisciplinaBean;
import com.bean.TurmaBean;
import com.bean.UnidadeBean;
import com.connection.DatabaseConnection;
import com.dao.BimestreDao;
import com.dao.DisciplinaDao;
import com.dao.TurmaDao;
import com.dao.UnidadeDao;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class AlterarActivity extends Activity {

	Spinner spUni;
	Spinner spDis;
	Spinner spTur;
	Spinner spBim;

	List<UnidadeBean> Uni;
	List<DisciplinaBean> Dis;
	List<TurmaBean> Tur;
	List<BimestreBean> Bim;

	ArrayAdapter<String> aListUni;
	ArrayAdapter<String> aListDis;
	ArrayAdapter<String> aListTur;
	ArrayAdapter<String> aListBim;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alterar);

		DatabaseConnection data = new DatabaseConnection(
				getApplicationContext());
		SQLiteDatabase escola = data.getWritableDatabase();

		Uni = new UnidadeDao().getAll(escola);
		List<String> listUni = new ArrayList<String>();
		for (int i = 0; i < Uni.size(); i++) {
			listUni.add(Uni.get(i).getNome());
		}

		aListUni = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, listUni);
		spUni = (Spinner) findViewById(R.id.escolaSpinner);
		spUni.setAdapter(aListUni);

		spUni.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				select(arg0, arg1, position, arg3);

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		escola.close();
	}

	public void select(AdapterView<?> arg0, View arg1, int position, long arg3) {
		DatabaseConnection data = new DatabaseConnection(
				getApplicationContext());
		SQLiteDatabase escola = data.getWritableDatabase();

		//insere a lista de disciplina na spinner disciplina
		Dis = new DisciplinaDao().getAll(escola, Uni.get(position).getId());
		List<String> listDis = new ArrayList<String>();
		for (int i = 0; i < Dis.size(); i++) {
			listDis.add(Dis.get(i).getNome());
		}
		aListDis = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, listDis);
		spDis = (Spinner)findViewById(R.id.disciplinaSpinner);
		spDis.setAdapter(aListDis);

		//insere a lista de turma na spinner turma
		Tur = new TurmaDao().getAll(escola, Uni.get(position).getId());
		List<String> listTur = new ArrayList<String>();
		for (int i = 0; i < Tur.size(); i++) {
			listTur.add(Tur.get(i).getNome());
		}
		aListTur = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, listTur);
		spTur = (Spinner)findViewById(R.id.turmaSpinner);
		spTur.setAdapter(aListTur);
		
		//insere a lista de bimestre na spinner bimestre
		Bim = new BimestreDao().getAll(escola, Uni.get(position).getId());
		List<String> listBim = new ArrayList<String>();
		for (int i = 0; i < Bim.size(); i++) {
			listBim.add(Bim.get(i).getNome());
		}
		aListBim = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, listBim);
		spBim = (Spinner)findViewById(R.id.bimestreSpinner);
		spBim.setAdapter(aListBim);
		
		escola.close();
	}

	public void acessar(View v) {
		Intent it = new Intent(getApplicationContext(),
				MainActivity.class);
		
		Bundle param = new Bundle();
		param.putString("unidade", Uni.get(spUni.getSelectedItemPosition()).getNome());
		param.putString("disciplina", Dis.get(spDis.getSelectedItemPosition()).getNome());
		param.putString("turma", Tur.get(spTur.getSelectedItemPosition()).getNome());
		param.putString("bimestre", Bim.get(spBim.getSelectedItemPosition()).getNome());
		
		param.putInt("id_unidade", Uni.get(spUni.getSelectedItemPosition()).getId());
		param.putInt("id_disciplina", Dis.get(spDis.getSelectedItemPosition()).getId());
		param.putInt("id_turma", Tur.get(spTur.getSelectedItemPosition()).getId());
		param.putInt("id_bimestre", Bim.get(spBim.getSelectedItemPosition()).getId());
		
		it.putExtras(param);
		startActivity(it);

	}

}
